#include "bmp.h"


Bmp::Bmp(int *lcd_p)
{
    memset(m_fileName,0,sizeof(m_fileName));

    m_lcdP =  lcd_p;
}

Bmp::Bmp(char *fileName,int *lcd_p)
{
    memset(m_fileName,0,sizeof(m_fileName));
    memcpy(m_fileName,fileName,strlen(fileName));

    m_lcdP =  lcd_p;

    //获取图片的属性
    fileInfo();
}

Bmp::Bmp(const char *fileName, int *lcd_p)
{
    memset(m_fileName,0,sizeof(m_fileName));
    memcpy(m_fileName,fileName,strlen(fileName));

    m_lcdP =  lcd_p;

    //获取图片的属性
    fileInfo();
}

Bmp::~Bmp()
{

}

void Bmp::setFileName(char *fileName)
{
    memset(m_fileName,0,sizeof(m_fileName));
    memcpy(m_fileName,fileName,strlen(fileName));

    //获取图片的属性
    fileInfo();
}

void Bmp::setFileName(const char *fileName)
{
    memset(m_fileName,0,sizeof(m_fileName));
    memcpy(m_fileName,fileName,strlen(fileName));

    fileInfo();
}

void Bmp::setFileName(std::string fileName)
{
    memset(m_fileName,0,sizeof(m_fileName));
    memcpy(m_fileName,fileName.data(),fileName.size());

    fileInfo();
}

//得到bmp图片的宽度
int Bmp::width()
{
    return m_width;
}
//得到bmp图片的高度
int Bmp::height()
{

    return m_height;
}
//得到bmp图片的大小
int Bmp::size()
{

    return m_size;
}

void Bmp::fileInfo()
{
    //2、打开bmp图片文件
    int bmpfd = open(m_fileName,O_RDONLY);
    if(bmpfd == -1)
    {
        perror("open bmpfile error");//perror将错误的原因打印出来
        return;
    }

    //读取bmp图片的文件头 + 信息头数据
    BITMAPFILEHEADER fileHead;
    BITMAPINFOHEADER infoHead;

    read(bmpfd,&fileHead,14);//读取14个字节
    read(bmpfd,&infoHead,40);//读取40个字节

    m_width = infoHead.biWidth;
    m_height = infoHead.biHeight;
    m_size = infoHead.biSizeImage+54;

    ::close(bmpfd);
}

void Bmp::setLcdMmapAddr(int *lcd_p)
{
    this->m_lcdP = lcd_p;
}

int Bmp::show(int x, int y)
{
    if(m_lcdP == NULL){
        std::cout<<"lcd not mmap,mmap addr is null"<<std::endl;
        return -1;
    }

    int i,j;


    //2、打开bmp图片文件
    int bmpfd = open(m_fileName,O_RDONLY);
    if(bmpfd == -1)
    {
        perror("open bmpfile error");//perror将错误的原因打印出来
        return -1;
    }

    //读取bmp图片的文件头 + 信息头数据
    BITMAPFILEHEADER fileHead;
    BITMAPINFOHEADER infoHead;

    read(bmpfd,&fileHead,14);//读取14个字节
    read(bmpfd,&infoHead,40);//读取40个字节

    m_width = infoHead.biWidth;
    m_height = infoHead.biHeight;
    m_size = infoHead.biSizeImage+54;

    //图片的宽度 * 每个像素点的字节数  一定是 4的倍数
//    if(m_width*3 != 4)
//    {
//        std::cout<<"图片的宽度 不是 4的倍数，请重新设置图片的宽度"<<std::endl;
//        ::close(bmpfd);
//        return -1;
//    }

    int tempbuf[m_width*m_height];


    //3、读取bmp图片的数据
    char bmpbuf[m_width*m_height*3];
    memset(bmpbuf,0,sizeof(bmpbuf));

    read(bmpfd,bmpbuf,sizeof(bmpbuf));
    //将bmp图片每个像素点的 B G R -->A R G  B
    for(i=0,j=0; i<m_width*m_height;i++,j+=3)
    {
        tempbuf[i] = 0x00<<24 | bmpbuf[j+2]<<16 | bmpbuf[j+1]<<8 | bmpbuf[j];
    }
    //上下180度颠倒
    for(j=0; j<m_height; j++)
    {
        for(i=0; i<m_width; i++)
        {
            m_lcdP[(j+y)*800+i+x] = tempbuf[(m_height-j-1)*m_width+i];
        }
    }

    close(bmpfd);

    return 0;
}

int Bmp::scaleShow(int x, int y, int scale)
{
    if(m_lcdP == NULL){
        std::cout<<"lcd not mmap,mmap addr is null"<<std::endl;
        return -1;
    }

    int i,j;


    //2、打开bmp图片文件
    int bmpfd = open(m_fileName,O_RDONLY);
    if(bmpfd == -1)
    {
        perror("open bmpfile error");//perror将错误的原因打印出来
        return -1;
    }

    //读取bmp图片的文件头 + 信息头数据
    BITMAPFILEHEADER fileHead;
    BITMAPINFOHEADER infoHead;

    read(bmpfd,&fileHead,14);//读取14个字节
    read(bmpfd,&infoHead,40);//读取40个字节

    m_width = infoHead.biWidth;
    m_height = infoHead.biHeight;
    m_size = infoHead.biSizeImage+54;

    //图片的宽度 * 每个像素点的字节数  一定是 4的倍数
    if(m_width*3 != 4)
    {
        std::cout<<"图片的宽度 不是 4的倍数，请重新设置图片的宽度"<<std::endl;
        ::close(bmpfd);
        return -1;
    }

    int tempbuf[m_width*m_height];
    int lcdbuf[m_width*m_height];

    //3、读取bmp图片的数据
    char bmpbuf[m_width*m_height*3];
    memset(bmpbuf,0,sizeof(bmpbuf));

    read(bmpfd,bmpbuf,sizeof(bmpbuf));
    //将bmp图片每个像素点的 B G R -->A R G  B
    for(i=0,j=0; i<m_width*m_height;i++,j+=3)
    {
        tempbuf[i] = 0x00<<24 | bmpbuf[j+2]<<16 | bmpbuf[j+1]<<8 | bmpbuf[j];
    }
    //上下180度颠倒
    for(j=0; j<m_height; j++)
    {
        for(i=0; i<m_width; i++)
        {
            lcdbuf[j*m_width+i] = tempbuf[(m_height-j-1)*m_width+i];
        }
    }
    //按照一定的比例裁剪图片
    int minbuf[(m_width/scale) * (m_height/scale)];//存储裁剪后的图片数据

    for(j=0; j<m_height/scale; j++) // for(j=0; j<120; j++)
    {
        for(i=0; i<m_width/scale; i++) // for(i=0; i<200; i++)
        {
            minbuf[j*(m_width/scale) + i] =  lcdbuf[j*scale*m_width + i*scale];
        }
    }
    //显示
    for(j=0; j<m_height/scale; j++)
    {
        for(i=0; i<m_width/scale; i++)
        {
           m_lcdP[(j+y)*800+i+x] =  minbuf[j*(m_width/scale) + i];
        }
    }
    close(bmpfd);

    return 0;
}
